/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/commercial
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may use this file in compliance with the License.
 * Alternatively, if you can't copy with the License, namely if you do not
 * want to provide source code of your modifications to this file, you may
 * negotiate a commercial licence with the initial developer.
 * You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is COID.
 *
 * The Initial Developer of the Original Code is
 * PosAm.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Brano Kemen
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "coid/comm/binstream/nullstream.h"

#include "coid/svc/coidsvc.h"

#include "service.h"
#include "serviceinst.h"
#include "server.h"

namespace coid {


////////////////////////////////////////////////////////////////////////////////
opcd ServiceCoid::instantiate( void** obj, ServiceInstanceCoid* svcin )
{
    InstanceData idt;
	idt._cmdin = &svcin->_cmdin;
    idt._flags = 0;
    //idt._hptimer = &SINGLETON(HPTIMER);
    idt._object = 0;

    opcd e = _desc.dispatch( &idt, StdDispatchMethod::CONSTRUCTOR, nullstream, 0 );
    if(!e)
        svcin->init( idt._object );

    return e;
}

////////////////////////////////////////////////////////////////////////////////
opcd ServiceCoid::spawn_auto_instance()
{
    local<ServiceInstanceCoid> inst = new ServiceInstanceCoid( &_desc, _path );

    void* optr=0;
    opcd e;
    e = instantiate( &optr, inst );

    if(!e)
    {
        inst->_flags |= ServiceInstanceCoid::fAUTOINSTANCE | ServiceInstanceCoid::fAUTHENTIFIED;

        //artifically increase number of locks to keep autoservice running
        inst->_mutex.inc_fast();

        NODEREF<ServiceInstanceCoid> svci;
        e = SRVROOTA.attach( inst.eject(), svci, _id, 0 );

        if(!e) {
            e = svci->dispatch( StdDispatchMethod::POSTCONSTRUCTOR, nullstream, 0 );
            DASSERT( e != ersNOT_IMPLEMENTED );     //old coidgen used, regenerate
        }

        for(;!e;)
        {
            e = svci->dpx_authentify_auto();
            if( e == ersFE_NOT_YET_READY )
            {
                sysMilliSecondSleep(500);
                e = 0;
            }
            else if(!e)  break;
        }

        if(e) {
            post_error(0) << "error starting autorun instance of {" << _desc._name << "} - " << opcd_formatter(e) << EOM;
            return e;
        }

        if( _desc._flags & ServiceDescriptor::fHAS_SPAWN_POINT )
            svci->dpx_spawn(true);
    
        svci.unset();
        post_info(0) << "started autorun instance of {" << _desc._name << "}" << EOM;
    }
    else
    {
        //inst->deinit(false);
        post_error(0) << "cannot start autorun instance of {" << _desc._name << "} - " << opcd_formatter(e) << EOM;
    }

    return e;
}

////////////////////////////////////////////////////////////////////////////////
opcd ServiceCoid::spawn_instance( binstream& arg )
{
    local<ServiceInstanceCoid> inst = new ServiceInstanceCoid( &_desc, _path );

    void* optr=0;
    opcd e;
    e = instantiate( &optr, inst );
    
    if(!e)
    {
        NODEREF<ServiceInstanceCoid> svci;
        e = SRVROOTA.attach( inst.eject(), svci, _id, 0 );

        if(!e) {
            e = svci->dispatch( StdDispatchMethod::POSTCONSTRUCTOR, nullstream, 0 );
            DASSERT( e != ersNOT_IMPLEMENTED );     //old coidgen used, regenerate
        }

        if(!e)
            e = svci->dpx_authentify(arg,0);

        if(e) {
            post_error(0) << "error starting instance of {" << _desc._name << "} - " << opcd_formatter(e) << EOM;
            return e;
        }

        post_info(0) << "started instance of {" << _desc._name << "} (" << svci->get_id() << ")" << EOM;

        svci.unset();
    }
    else
    {
        //inst->deinit(false);
        post_error(0) << "cannot start instance of {" << _desc._name << "} - " << opcd_formatter(e) << EOM;
    }
    
    return e;
}


} //namespace coid
